package com.chuangjiangx.member.manager.web.web.score.controller;

import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.basic.dao.model.InMbrConfig;
import com.chuangjiangx.member.basic.ddd.query.MbrConfigQuery;
import com.chuangjiangx.member.manager.basic.web.controller.BaseController;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.basic.web.interceptor.Permissions;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.manager.web.web.score.request.ModifyScoreGrandRuleRequest;
import com.chuangjiangx.member.manager.web.web.score.response.QueryScoreGrantRuleResponse;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGrandTotalRule;
import com.chuangjiangx.member.score.ddd.application.MbrScoreGrandTotalRuleApplication;
import com.chuangjiangx.member.score.ddd.application.command.ModifyScoreGrandTotalRuleCommand;
import com.chuangjiangx.member.score.ddd.query.MbrScoreGrandTotalRuleQuery;
import com.sun.jmx.snmp.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 累计积分规则,商户权限
 */
@RestController
@RequestMapping(value = "/api/mbr/score-grant-rule", produces = "application/json")
public class MbrScoreGrandTotalRuleController extends BaseController {


    @Autowired
    private MbrScoreGrandTotalRuleApplication mbrScoreGrandTotalRuleApplication;

    @Autowired
    private MbrScoreGrandTotalRuleQuery mbrScoreGrandTotalRuleQuery;
    @Autowired
    private MbrConfigQuery mbrConfigQuery;

    /**
     * 查询当前商户下的积分累计规则
     *
     * @return 累计规则(包含关注公众号赠送积分及是否储值消费累计积分配置信息)
     */
    @RequestMapping(value = "")
    @Login
    @Permissions("50023")
    public Response queryScoreGrantRule() {
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        InMbrScoreGrandTotalRule scoreGrandTotalRule = mbrScoreGrandTotalRuleQuery.queryScoreGrandTotalRuleByMerchantId(user.getMerchantId());
        InMbrConfig inMbrConfig = mbrConfigQuery.queryByMerchantId(user.getMerchantId());
        QueryScoreGrantRuleResponse response = new QueryScoreGrantRuleResponse();
        response.setConfig(inMbrConfig);
        response.setScoreGrandTotalRule(scoreGrandTotalRule);
        return ResponseUtils.successCamel(response);
    }

    /**
     * 修改积分累计规则
     *
     * @param session 获取用户信息session
     * @return 修改结果
     * @throws Exception
     */
    @RequestMapping(value = "/modify")
    @Login
    @Permissions("50021")
    public Response modify(
                           @Validated @RequestBody ModifyScoreGrandRuleRequest request) throws Exception {
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        ModifyScoreGrandTotalRuleCommand command = new ModifyScoreGrandTotalRuleCommand();
        command.setMerchantId(user.getMerchantId());
        command.setAmount(request.getAmount());
        command.setScore(request.getScore());
        command.setSubscribePnGiftScore(request.getSubscribePnGiftScore());
        command.setCardConsumerGrantScore(request.getCardConsumerGrantScore());
        mbrScoreGrandTotalRuleApplication.addAndModify(command);
        return ResponseUtils.successCamel();
    }

}
